package org.hong.samples.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.hong.samples.model.Pager.SystemContext;

public class PagerFilter implements Filter {
	private int pageSize;
	private int offset;
	@Override
	public void destroy() {
	}
	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
	try {
	offset = Integer.parseInt(request.getParameter("page.offset"));
	} catch (NumberFormatException
	e) {
	}
	try {
	SystemContext.setOffset(offset);
	SystemContext.setpageSize(pageSize);
	chain.doFilter(request, response);
	} finally {
	SystemContext.removeOffset();
	SystemContext.removepageSize();
	}
	}
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	try {
	pageSize = Integer.parseInt(filterConfig.getInitParameter("pageSize"));
	} catch (NumberFormatException e) {
	pageSize = 5;
	}
	}
}
